from decimal import Decimal
from django.db import models
import os

class CurrencyField(models.DecimalField):
    __metaclass__ = models.SubfieldBase
    
    def __init__(self, *args, **kwargs):
        kwargs['max_digits'] =  8
        kwargs['decimal_places'] = 2
        super(CurrencyField, self).__init__(*args, **kwargs)

    def to_python(self, value):
        try:
            return super(CurrencyField, self).to_python(value).quantize(Decimal('0.01'))
        except AttributeError:
            return None
        
def maxTimeXmlFile(files,index,maxtime,maxfile,directory):
        if len(files) == index: return maxfile
        mtime = os.stat(directory+files[index]).st_mtime
        if mtime>maxtime:
            maxtime = mtime
            maxfile = files[index]
        return maxTimeXmlFile(files,index+1,maxtime,maxfile,directory)

